<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入id" suffix-icon="el-icon-search" v-model="pbId"></el-input>
      <el-input style="width: 200px" placeholder="请输入投放点id" suffix-icon="el-icon-search" v-model="pbssId" class="ml-5"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <!--      <el-upload action="http://localhost:9090/file/upload" :show-file-list="false" :on-success="handleFileUploadSuccess" style="display: inline-block">-->
      <!--        <el-button type="primary" class="ml-5">上传文件 <i class="el-icon-top"></i></el-button>-->
      <!--      </el-upload>-->


    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'" >
      <el-table-column prop="pbId" label="pbId" width="80"></el-table-column>
      <el-table-column prop="pbName" label="充电宝名称"></el-table-column>
      <el-table-column prop="pbssId" label="所属投放点id"></el-table-column>
      <el-table-column prop="pbssName" label="所属投放点名称"></el-table-column>
      <el-table-column prop="pbPrice" label="价格"></el-table-column>
      <el-table-column prop="pbStatus" label="租借状态" :formatter="formatBoolean" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!--          {{scope.row.pbStatus ? "已租借":"未租借"}}-->
          <el-tag type="danger" v-if="scope.row.pbStatus==true">
            已租借
          </el-tag>
          <el-tag type="success" v-if="scope.row.pbStatus==false">
            未租借
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" v-if="scope.row.pbStatus===false" >租借<i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>

    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="确任订单信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户名" >
          <el-input v-model="user.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户手机" >
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="充电宝名称" >
          <el-input v-model="form.pbName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="投放点名称" >
          <el-input v-model="form.pbssName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="sendDirect" >直接支付</el-button>
        <el-button type="primary" @click="sendNone" >免押金支付</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/util/request";

export default {
  name: "Pb",
  data() {
    return {
      tableData: [],
      pbId:'',
      pbssId:'',
      multipleSelection: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      dialogFormVisible:false,
      form:{
        userName:'',
        phone:'',
        pbName:'',
        pbssName:''
      },
      isDisabled: false,
      options:[],
      orderInfo:{},
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  created() {
    this.load(),
        this.getPbssName()
  },
  methods: {

    async getUser(){
      return  (await request.get("/user/userId/" + this.user.userId)).data;
    },
    load() {
      this.request.get("/user/pbPage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          pbssId:this.pbssId,
          pbId:this.pbId

        }
      }).then(res => {
        if(res.code==='200'){
          this.tableData = res.data.records
          this.total = res.data.total
        }else {
          // this.$message.error(res.msg)
        }


      })
    },
    sendDirect(){
      this.request.post("/user/Pb/sendDirect",this.orderInfo).then(res=>{
        if (res.code=='200'){
          this.$message.success("租借成功！")
          this.dialogFormVisible=false;
          this.load(),
              this.orderInfo='',
              this.isDisabled=true
          //更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }else if(res.code=='600'){
          this.$message.error("该充电宝已被租借")
          this.dialogFormVisible=false;
        }
        else if(res.code=='500'){
          this.$message.error("用户余额不足，请充值再试")
          this.dialogFormVisible=false;
        }
        else  {
          this.$message.error("租借失败")
        }
      })
    },
    sendNone(){
      this.request.post("/user/Pb/sendNone",this.orderInfo).then(res=>{
        if (res.code=='200'){
          this.$message.success("免押金租借成功！")
          this.dialogFormVisible=false;
          this.load(),
              this.orderInfo=''
          this.isDisabled=true
          //更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }
        else if(res.code=='600'){
          this.$message.error("该充电宝已被租借")
          this.dialogFormVisible=false;
        }
        else if(res.code=='500'){
          this.$message.error("用户余额不足，请充值再试")
          this.dialogFormVisible=false;
        }
        else {
          this.$message.error("免押金租借失败,请验证完身份再试")
          this.dialogFormVisible=false;
        }
      })
    },


    reset() {
      this.pbssName = "",
          this.pbssId=''
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },

    handleEdit(row){
      this.dialogFormVisible=true,
          this.form={
              userName: this.user.userName,
               pbName: row.pbName,
            pbssName: row.pbssName,
            phone: this.user.phone

          }
      this.orderInfo=this.form
      console.log(this.orderInfo)
    }

    // handleFileUploadSuccess(res) {
    //   console.log(res)
    //   this.load()
    // },
    // download(url) {
    //   window.open(url)
    // }
    // formatBoolean: function (row) {
    //   let ret = ''
    //   if (row.sendStatus === true) {
    //     ret = "已租借" //根据自己的需求设定
    //   } else {
    //     ret = "未租借"
    //   }
    //   return ret;
    // }

  }
}
</script>

<style scoped>

</style>